<template>
  <div class="member-admin animate__animated animate__fadeInUp">
    <!-- 系统设置成员管理导航栏 -->
    <div class="datr animate__animated animate__fadeInUp animate__delay-0.5s">
      <p class="ga"></p>
      <span class="chryu">公告管理</span>
    </div>
    <el-card shadow="never">
      <!-- 系统设置成员管理搜索查询添加 -->
      <div
        class="d-flex animate__animated animate__fadeInUp animate__delay-0.9s"
      >
        <p>
          <el-input placeholder="消息标题" v-model="input" clearable>
          </el-input>
        </p>
        <!-- <p class="ml-1">
          <el-select v-model="value" placeholder="全部">
            <el-option
              v-for="item in shijain"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
        </p> -->
        <p class="ml-1"></p>
        <!-- <div class="block">
          <el-date-picker
            v-model="value2"
            type="monthrange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </div> -->
        <!-- <div class="block">
          <el-date-picker
            v-model="value2"
            type="datetime"
            placeholder="选择日期时间"
            align="right"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </div> -->
        <div class="block">
          <el-date-picker
            v-model="value2"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
          >
          </el-date-picker>
        </div>
        <p class="ml-1">
          <el-button
            type="primary"
            icon="el-icon-search"
            :loading="isloading"
            @click="mohuchaxu()"
            >查询</el-button
          >
        </p>
        <!-- 添加公告 -->
        <p class="ml-1">
          <!-- <el-button plain>添加公告</el-button> -->
          <el-button @click="tianjia">添加公告</el-button>
        </p>
      </div>
      <!-- 表格渲染 -->
      <p class="mt-1">
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          border
          :row-key="getRowKey"
          :header-cell-style="{ textAlign: 'center' }"
          :cell-style="{ textAlign: 'center' }"
          @selection-change="handleSelectionChange"
        >
          <el-table-column :reserve-selection="true" type="selection" width="55"> </el-table-column>
          <el-table-column label="编号" width="100">
            <template slot-scope="scope">{{ scope.row.id }}</template>
          </el-table-column>
          <el-table-column prop="msgTitle" label="消息标题" width="400">
          </el-table-column>
          <el-table-column
            prop="created"
            label="发布时间"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="createdPerson"
            label="发布人员"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="120">
            <template slot-scope="scope">
              <el-button
                @click="handleClick(scope.row, scope.$index, tableData)"
                type="text"
                size="small"
                >查看</el-button
              >
              <el-button @click="open(scope.row)" type="text" size="small"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </p>

      <div class="d-flex v-center">
        <div style="margin-top: 20px">
          <el-button @click="toggleSelection(tableData)">反选</el-button>
          <el-button @click="toggleSelection()">取消选择</el-button>
        </div>
        <p class="mt-2 ml-1">
          <el-select v-model="value" clearable placeholder="批量操作">
            <el-option
              v-for="item in plcz"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </p>
        <p class="mt-2 ml-1">
          <el-row>
            <el-button @click="piliangcaozuo">确定</el-button>
          </el-row>
        </p>

        <!-- 添加弹框 -->
        <el-dialog
          :append-to-body="true"
          title="公告添加"
          :visible.sync="dialogFormVisible"
        >
          <el-form :model="form">
            <el-form-item
              required
              label="公告标题"
              :label-width="formLabelWidth"
            >
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item
              required
              label="公告内容"
              :label-width="formLabelWidth"
            >
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入内容"
                v-model="form.region"
                autocomplete="off"
              >
              </el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="tianjiaquxiao">取 消</el-button>
            <el-button type="primary" @click="tianjiaqueren">确 定</el-button>
          </div>
        </el-dialog>

        <!-- 查看弹框 -->
        <el-dialog
          :append-to-body="true"
          title="查看公告"
          :visible.sync="dialogFormVisible2"
          style=""
        >
          <div>
            <p class="p1 d-flex j-center">{{ this.chakan.createdPerson }}</p>
            <p class="p2 d-flex j-center mt-1 mb-1">
              {{ this.chakan.created }}
            </p>
            <hr />
            <p class="p3 d-flex mt-1">{{ this.chakan.msgText }}</p>
          </div>
        </el-dialog>
      </div>
      <!-- 页码点击 -->
      <div class="t-right">
        <el-pagination
          background
          layout="prev, pager, next"
          :page-size="limit"
          :total="total"
          @current-change="diji"
        >
        </el-pagination>
      </div>

      <!-- 多选框 -->
    </el-card>
  </div>
</template>
<script>
import moment from "moment";
moment.locale("zh-cn");
let cityOptions = [" "]; //多选框
export default {
  data() {
    return {
      leng:0,//数据的长度
      // 多选框

      limit: 6, //公告渲染页数
      total: 0, //公告渲染总数数
      page: 1, //当前页
      isloading: false, //搜索框图标显示
      input: "", //输入框的
      value: "", //下拉框的值
      value2: "", //时间
      value4: "", //开始时间
      value3: "", //结束时间
      multipleSelection: [], //表格多选框数组

      // 添加弹框数据
      dialogFormVisible: false,
      form: {
        name: "",
        region: "",
      },
      formLabelWidth: "100px",

      // 查看弹框数据
      dialogFormVisible2: false,
      chakan: [],

      //公告位置下拉框的值
      shijain: [
        {
          value: "黄金糕",
          //   label: "黄金糕",
        },
        {
          value: "双皮奶",
          //   label: "双皮奶",
          //   disabled: true,
        },
        {
          value: "蚵仔煎",
          //   label: "蚵仔煎",
        },
        {
          value: "龙须面",
          //   label: "龙须面",
        },
        {
          value: "北京烤鸭",
          //   label: "北京烤鸭",
        },
      ],
      //   渲染表格
      tableData: [],
      // 批量操作
      plcz: [
        {
          value: "删除",
          //   label: "蚵仔煎",
        },
      ],
      //时间框
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      },
    };
  },
  methods: {
    //批量多页面的操作
    getRowKey(row){
         return row.id;
    }, 
    //点击页面函数
    diji(page) {
      this.page = page;
      this.chaXun();
      // this.total=this.tableData.length
      // console.log(this.page,this.total)
    },

    // 查询函数
    chaXun() {
      // console.log(111);
      this.$axios({
        url: "/gym/notice/list",
        method: "post",
        data: {
          page: this.page,
          limit: this.limit,
        },
      }).then((res) => {
        // console.log(res);
        this.leng=res.data.data.length
        this.tableData = res.data.data;
        this.total = res.data.count;
        for (let t in this.tableData) {
          this.tableData[t].created = moment(this.tableData[t].created).format(
            "YYYY-MM-DD HH:mm:ss:ms"
          );
          this.tableData[t].startTime = moment(
            this.tableData[t].startTime
          ).format("YYYY-MM-DD HH:mm:ss:ms");
        }
        // console.log(moment("2021-06.08 8:25:12").format("x"))
      });
    },

    // 表格选择
    toggleRowSelection(row, selected) {
      console.log(row, selected, 11111);
    },

    toggleSelection(rows) {
      // this.multipleSelection=this.tableData;

      // console.log(this.multipleSelection,22222);
      // console.log(rows,333333)
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection=[];
     this.multipleSelection=val;
      // this.multipleSelection = val;
      // console.log(this.multipleSelection.toString(), 44444);

    },

    //添加公告
    tianjia() {
      this.dialogFormVisible = true;
    },
    // 添加取消
    tianjiaquxiao() {
      this.dialogFormVisible = false;
      this.$message({
        type: "info",
        message: "已取消添加",
      });
    },
    // 添加确认是
    tianjiaqueren() {
      if (this.form.name == "" || this.form.region == "") {
        this.$message({
          type: "warning",
          message: "不能有空字段",
        });
      } else {
        this.$axios({
          url: "/gym/notice/add",
          method: "post",
          data: {
            msgTitle: this.form.name,
            msgText: this.form.region,
          },
        })
          .then((res) => {
            this.dialogFormVisible = false;
            if (
              (res.data.code >= 200 && res.data.code < 300) ||
              res.data.code == 304
            ) {
              (this.form.name = ""),
                (this.form.region = ""),
                // console.log(111);
                this.$message({
                  type: "success",
                  message: "添加成功!",
                });
              this.form.name == "";
              this.form.region == "";
              this.chaXun();
            } else {
              this.$message.error("添加失败");
            }
          })
          .catch(() => {});
      }
    },
    // 查看
    handleClick(v, index, chaxun) {
      // console.log(v.id)
      this.$axios({
        url: "/gym/notice/find",
        method: "get",
        params: {
          id: v.id,
        },
      })
        .then((res) => {
          // console.log(res);
          this.chakan = res.data.data;

          this.chakan.created = moment(this.chakan.created).format(
            "YYYY-MM-DD HH:mm:ss:ms"
          );

          console.log(this.chakan);

          if (
            (res.data.code >= 200 && res.data.code < 300) ||
            res.data.code == 304
          ) {
            this.$message({
              type: "success",
              message: "查看成功!",
            });
          } else {
            this.$message.error("查看失败");
          }

          // console.log(moment("2021-06.08 8:25:12").format("x"))
        })
        .catch(() => {});

      // console.log(v[0], index, chaxun);
      this.chakan = v;
      // console.log(this.chakan);
      this.dialogFormVisible2 = true;
    },
    // 删除
    open(v) {
      console.log(v);
      //  this.dialogFormVisible = true;

      this.$confirm("确定要删除该公告吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios({
            url: "/gym/notice/delete",
            method: "get",
            params: {
              id: v.id,
            },
          })
            .then((res) => {
              console.log(res.data);
              if (
                (res.data.code >= 200 && res.data.code < 300) ||
                res.data.code == 304
              ) {
                // console.log(111);
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                if(this.leng-1==0){
                  this.page--;
                }
               if(this.page<=1){
                 this.page=1;
               }
                this.chaXun();
              } else {
                this.$message.error("删除失败");
              }
            })
            .catch(() => {});
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //模糊查询
    mohuchaxu() {
      console.log(this.input);
      console.log(this.value2);
      console.log(this.value2[1]);
      console.log(this.value2[0]);
      // console.log(this.input);
      // console.log(2222222,this.value2 == "Invalid date",this.value2)

      if (
        (this.value2 == "" || this.value2 == "Invalid date") &&
        this.input == ""
      ) {
        // console.log(111)
        //this.value2=""
        this.chaXun();
      } else if (this.value2 == "" || this.value2 == "Invalid date") {
        // console.log(this.value2[0])

        this.value3 = moment(this.value2[0]).format("x");
        this.value4 = moment(this.value2[1]).format("x");
        if (this.value2 == "" || this.value2 == "Invalid date") {
          this.value3 = "";
          this.value4 = "";
        }

        this.value2 = "";
        this.$axios({
          url: "/gym/notice/list",
          method: "post",
          data: {
            page: this.page,
            limit: this.limit,
            startTime: this.value3,
            endTime: this.value4,
            msgTitle: this.input,
          },
        }).then((res) => {
          // console.log(this.value2);
          // console.log(res);
          this.tableData = res.data.data;
          // console.log(this.tableData);
          this.total = res.data.count;
          for (let t in this.tableData) {
            this.tableData[t].endTime = moment(
              this.tableData[t].endTime
            ).format("YYYY-MM-DD HH:mm:ss:ms");
            this.tableData[t].startTime = moment(
              this.tableData[t].startTime
            ).format("YYYY-MM-DD HH:mm:ss:ms");
          }
          // console.log(moment(1625642112000).format("YYYY-MM-DD HH:mm:ss:ms"))
        });
      } else {
        this.value3 = moment(this.value2[0]).format("x");
        this.value4 = moment(this.value2[1]).format("x");

        this.$axios({
          url: "/gym/notice/list",
          method: "post",
          data: {
            page: this.page,
            limit: this.limit,
            startTime: this.value3,
            endTime: this.value4,
            msgTitle: this.input,
          },
        }).then((res) => {
          this.tableData = res.data.data;
          this.total = res.data.count;
          for (let t in this.tableData) {
            this.tableData[t].created = moment(
              this.tableData[t].created
            ).format("YYYY-MM-DD HH:mm:ss:ms");
            this.tableData[t].startTime = moment(
              this.tableData[t].startTime
            ).format("YYYY-MM-DD HH:mm:ss:ms");
          }
        });
      }
    },
    // 批量操做
    piliangcaozuo() {
      console.log(this.multipleSelection);
      // console.log(this.value);
      // if (this.value == "下线") {
      //   this.value = 0;
      // } else if (this.value == "上线") {
      //   this.value = 1;
      // } else if (this.value == "删除") {
      //   this.value = 2;
      // }

      // if (this.value == "") {
      //   this.$message.error("没有选操作项或者是没选操作内容");
      // }
      //  if{
      // if (this.value == 0 || this.value == 1) {


         let ffff=[]
      for (let i = 0; i < this.multipleSelection.length; i++) {
        ffff.push(this.multipleSelection[i].id);
      };
        this.$axios({
          url: "/gym/notice/deleteByIds",
          method: "post",
          data: {
            ids:ffff.toString(),
          },
        })
          .then((res) => {
            if (
              (res.data.code >= 200 && res.data.code < 300) ||
              res.data.code == 304
            ) {
              this.multipleSelection=[];
              this.$message({
                type: "success",
                message: "操作成功!",
              });
             this.$refs.multipleTable.clearSelection();
              if(this.leng==ffff.length){
                  this.page--;
                }
               if(this.page<=1){
                 this.page=1;
               }
                
               ffff=[];
              this.chaXun();
            } else {
              this.$message.error("操作失败");
            }
          })
          .catch(() => {});
    },
  },

  // 创建前
  brforeCreate() {},
  // 创建后
  created() {},
  // 挂载前
  beforeMount() {
    this.chaXun();
  },
  // 挂载后
  mounted() {
    // this.diji()
    // console.log(555555);
  },
  // 数据更新前
  beforeUpdate() {},
  // 数据更新后
  updated() {},
  // 销毁前
  beforeDestroy() {},
  // 销毁后
  destroyed() {},
};
</script>

<style scoped lang="less">
// 导航栏css
.datr {
  display: flex;
  align-items: center;
  width: 100%;
  height: 50px;
  background: linear-gradient(
    to right,
    rgba(17, 17, 17, 0.619),
    rgba(14, 13, 13, 0.251)
  );
  border-radius: 8px 8px 0px 0px;

  .ga {
    display: flex;
    align-items: center;
    width: 5px;
    height: 35px;
    background-color: rgba(0, 0, 255);
    // line-height: 50px;
    margin-left: 10px;
  }
  .chryu {
    display: inline-block;
    width: 80px;
    height: 50px;
    line-height: 50px;
    color: rgb(247, 243, 243);
    font-size: 17px;
    font-weight: bold;
  }
}

// 公告输入框的样式
.el-form-item {
  /deep/.el-textarea__inner {
    height: 150px !important;
  }
}

// 公告查看框的样式
.p1 {
  font-size: 20px;
}
.p3 {
  height: 200px;
}
</style>